package gewu.suleju.controller;

import gewu.suleju.model.pojo.User;
import gewu.suleju.model.vo.EmailCode;
import gewu.suleju.model.vo.PhoneCode;
import gewu.suleju.model.vo.Result;
import gewu.suleju.model.vo.UserNamePicVo;
import gewu.suleju.rbm.RbmConst;
import gewu.suleju.sevice.imp.UserServiceImp;
import gewu.suleju.util.JWTUtil;
import gewu.suleju.util.MailUtil;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

//@CrossOrigin
@Controller
@RequestMapping("userLog")
public class UserLogController {
    @Autowired
    private UserServiceImp userServiceImp;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ResponseBody
    @GetMapping("checkCode")
    public Result checkCode(String email){
        if(email==null){
            return new Result("500","邮箱不能为空");
        }
        //查询登录的人
        User user = userServiceImp.findUserByEmail(email);
        String checkCode = "";

        if(user!=null){
            //生成验证码
            checkCode = String.valueOf((int)(Math.random()*900000+100000));



            EmailCode emailCode = new EmailCode(email,checkCode);
            rabbitTemplate.convertAndSend(RbmConst.SIMPLE_QUEUE,emailCode);//传入消费者名
            //将code和Email存到redis
            redisTemplate.opsForValue().set(email,checkCode,60, TimeUnit.SECONDS);
            return new Result("200","验证码发送成功!",checkCode);
        }
        return new Result("501","该用户不存在，请注册");
    }
    @ResponseBody
    @GetMapping("log")
    public Result log(String inputemail,String inputcode){
        User user = userServiceImp.findUserByEmail(inputemail);
        if(user != null){
            if(!redisTemplate.hasKey(inputemail)){
                return new Result("501","你的验证码已经过期了，请重新获取验证码！");
            }
            String code = (String) redisTemplate.opsForValue().get(inputemail);
            if(code.equals(inputcode)){
                String token = JWTUtil.geneJsonWebToken(user);
                return new Result("200","登录成功！",token);
            }
            return new Result("502","验证码不正确!");
        }
        return new Result("500","你还没有注册，请先注册！");
    }
    @ResponseBody
    @GetMapping("isLog")
    public Result isLog(HttpServletRequest request, HttpServletResponse response){

        String token = request.getHeader("token");
        if(token.equals(null)||token.trim().length() == 0) {
            return new Result("500","sorry,请先登录!");
        }
        Claims claims = JWTUtil.checkJWT(token);
        UserNamePicVo user = new UserNamePicVo();
        user.getHeadPicAddress();
        user.setUsername((String)claims.get("username"));
        return new Result("200","登录成功！",user);
    }
    @ResponseBody
    @GetMapping("checkphone")
    public Result phone(String phone) {
        if (phone == null || phone.length() == 0) {
            return new Result("500", "手机号不能为空");
        }
        //查询登录的人
        User user = userServiceImp.findUserByPhone(phone);
        String checkphone = "";
        if (user != null) {
            //生成验证码
            checkphone = String.valueOf((int) (Math.random() * 900000 + 100000));

            PhoneCode phoneCode = new PhoneCode(phone,checkphone);
            rabbitTemplate.convertAndSend(RbmConst.SIMPLE_QUEUE01,phoneCode);//传入消费者名
            //将code和phone存到redis
            redisTemplate.opsForValue().set(phone, checkphone, 60, TimeUnit.SECONDS);
            return new Result("200", "验证码发送成功",checkphone);
        }
        return new Result("501", "该用户不存在，请注册");

    }
    //   账户登陆
    @ResponseBody
    @GetMapping("checkAccount")
    public Result checkaccount(String username, String password) {
        if (username == null || password == null) {
            return new Result("500", "账户或密码不能为空");
        }
        //查询登录的人
        User user = userServiceImp.findUserByUsername(username);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                String token = JWTUtil.geneJsonWebToken(user);
                return new Result("200", "登陆成功",token);
            } else {
                return new Result("400", "账户或密码错误");
            }

        }
        return new Result("501", "该用户不存在，请注册");
    }
    //手机登陆
    @ResponseBody
    @GetMapping("phone_log")
    public Result phone_log(String inputphone, String phonecode) {
        User user = userServiceImp.findUserByPhone(inputphone);
        if (user != null) {
            if (!redisTemplate.hasKey(inputphone)) {
                return new Result("501", "你的验证码已经过期了，请重新获取验证码！");
            }
            String code = (String) redisTemplate.opsForValue().get(inputphone);
            if (code.equals(phonecode)) {
                String token = JWTUtil.geneJsonWebToken(user);
                return new Result("200", "登录成功！", token);
            }
            return new Result("502", "验证码不正确!");
        }
        return new Result("500", "你还没有注册，请先注册！");
    }


}